package javaweblearn;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//删除cookie
public class CookieDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //存储中文，必须使用URLEncoder类里面的encode(String s, String enc)方法进行中文转码
        //Cookie cookie = new Cookie("userName", URLEncoder.encode("孤傲苍狼", "UTF-8"));
        //response.addCookie(cookie);
        //获取cookie中的中文数据时，再使用URLDecoder类里面的decode(String s, String enc)进行解码
//        URLDecoder.decode(cookies[i].getValue(), "UTF-8")

        Cookie cookie = new Cookie("lastAccessTime",System.currentTimeMillis()+"");
        //将cookie的有效期设置为0，命令浏览器删除该cookie
        cookie.setMaxAge(0);
        resp.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
